package com.ddwanglife.bilibili;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

public class CreateVedioMenu {

    //获取路径下所有文件夹的项目名称
    public static void main(String[] args) {
        File bootfold = new File("J:\\哔哩哔哩");
        String projectMenuName = "menu.txt";
        File menuFile = new File(bootfold+"\\"+projectMenuName);
        if(menuFile.exists()){
            System.out.println("如果文件存在，则删除menu目录："+menuFile.delete());
        }
        if(bootfold.exists()&&bootfold.listFiles().length>0){
            for (File file : bootfold.listFiles()){
                String projectNumber = file.getName();//获取文件编号
                System.out.println("--"+projectNumber);
                if(file.isDirectory()){
                    File[] vedioFiles = file.listFiles();
                    //获取第一个文件
                    if(vedioFiles != null && vedioFiles.length>0){
                        //获取子文件entry.json
                        File[] entrys = vedioFiles[0].listFiles(new FileFilter() {
                            @Override
                            public boolean accept(File pathname) {
                                return pathname.getName().contains("entry");
                            }
                        });
                        if(entrys != null && entrys.length >0){
                            //如果entry存在，则解析
                            String jsonString = null;
                            try {
                                jsonString = FileUtils.readFileToString(entrys[0]);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            JSONObject jsonObject = JSONObject.parseObject(jsonString);
                            CreateVedioDetailDirector.appendContent(bootfold+"\\"+projectMenuName,projectNumber+"\n"+"--"+jsonObject.getString("title")+"\n");
                        }
                    }
                }
            }
        }
    }
}
